/*
 * 用户注册界面
 */
package com.wonyesoft.wuliu.acticity;

import java.util.HashMap;
import java.util.Map;

import tools.CheckVersion;
import tools.DownLoadAPP;
import tools.SD_Finder;
import View.InPutDialog;
import View.InPutDialog.getUserIntent;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;

import com.baidu.location.LocationClient;
import com.google.gson.Gson;
import com.google.zxing.CaptureActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.SPUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class SettingActivity extends BaseActivity implements OnClickListener {

	private LinearLayout gonggao, gengxin, about_us, tousu, settings_update;
	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private AppContent app;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		title = (TextView) findViewById(R.id.head_title);
		gonggao = (LinearLayout) findViewById(R.id.gonggao);
		gengxin = (LinearLayout) findViewById(R.id.gengxin);
		about_us = (LinearLayout) findViewById(R.id.about_us);
		tousu = (LinearLayout) findViewById(R.id.tousu);
		View view = findViewById(R.id.exitcehdhz);
		view.setOnClickListener(this);
		gonggao.setOnClickListener(this);
		gengxin.setOnClickListener(this);
		about_us.setOnClickListener(this);
		tousu.setOnClickListener(this);
		title.setVisibility(View.VISIBLE);
		title.setText("设置");
		app = (AppContent) getApplication();
		app.addActivity(this);
		TextView settings_edition = (TextView) findViewById(R.id.settings_edition);	
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info;
			info = manager.getPackageInfo(this.getPackageName(), 0);
			String ver = info.versionName;
			settings_edition.setText("版本号：" + ver);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SettingActivity.this.finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.gonggao:
			openActivity(GongGaoActivity.class);
			break;
		case R.id.gengxin:
			CheckVersion();
			break;
		case R.id.about_us:
			openActivity(AboutUsActivity.class);
			break;
		case R.id.tousu:
			openActivity(ZiFeiActivity.class);
			break;
		case R.id.settings_update:
			CheckVersion();
		case R.id.exitcehdhz:
			exitCEHD();
		default:
			break;
		}
	}

	/**
	 * 
	 * @Title: CheckVersion
	 * @Description: TODO(APP检查版本)
	 * @param @throws NameNotFoundException 参数说明
	 * @return void 返回类型
	 * @throws
	 */
	private void CheckVersion() {

		CheckVersion.check(this, new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					showDonwLoad();
				}else {
					Toast.makeText(SettingActivity.this, "已经是最新版本", Toast.LENGTH_LONG).show();
				}
			}
		});

		// int verCode =
		// this.getPackageManager().getPackageInfo(getPackageName(),
		// 0).versionCode;
		// System.out.println("版本号：" + verCode);
		// try {
		// int verCode = this.getPackageManager().getPackageInfo(
		// getPackageName(), 0).versionCode;
		// Map<String, Object> dataMap = new HashMap<String, Object>();
		// JsonRequest jr = JsonRequest.getInit();
		// dataMap.put("versionNumber", verCode + "");
		// dataMap.put("versionType", "司机版本");
		// jr.setData(dataMap);
		// final Gson gson = new Gson();
		// String json = gson.toJson(jr);
		// // System.out.println(json);
		// HttpUtils http = new HttpUtils();
		// RequestParams params = new RequestParams();
		// params.addBodyParameter("jsonParam", json);
		// http.send(HttpRequest.HttpMethod.POST, GagApi.CheckVersion, params,
		// new RequestCallBack<String>() {
		//
		// @Override
		// public void onSuccess(ResponseInfo<String> responseInfo) {
		// // testTextView.setText("reply: " +
		// // responseInfo.result);
		// JsonResponse jrs = gson.fromJson(
		// responseInfo.result, JsonResponse.class);
		// // System.out.println("升级 + " +
		// // responseInfo.result);
		// State state = jrs.getState();
		// Log.v("去更新1","去更新");
		// if (state.getCode().equals(State.SUCEECCODE)) {
		// // Log.v("去更新2","去更新");
		// if (GagApi.UPAPKPATH != null) {
		//
		// // 创建退出对话框
		// LayoutInflater inflater = LayoutInflater
		// .from(SettingActivity.this);
		// LinearLayout layout = (LinearLayout) inflater
		// .inflate(R.layout.alert_dialog,
		// null);
		// final Dialog isExit = new AlertDialog.Builder(
		// SettingActivity.this).create();
		// isExit.show();
		// isExit.setContentView(layout);
		// // 设置对话框标题
		// TextView title = (TextView) layout
		// .findViewById(R.id.title);
		// TextView message = (TextView) layout
		// .findViewById(R.id.message);
		// title.setText("消息提醒");
		// // 设置对话框消息
		// message.setText("有新的版本更新");
		// Button check = (Button) layout
		// .findViewById(R.id.check);
		// Button giveup = (Button) layout
		// .findViewById(R.id.giveup);
		// check.setText("下载");
		// giveup.setText("忽略");
		// check.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// Intent intents = new Intent();
		// intents.setClass(
		// SettingActivity.this,
		// DownLoadAPP.class);
		// String root=SD_Finder.GetPaht();
		// if (root!=null&&root.length()>0) {
		// intents.putExtra("path", root);
		// startService(intents);
		// isExit.dismiss();
		// }
		// }
		// });
		// giveup.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// // TODO Auto-generated method stub
		// isExit.dismiss();
		// }
		// });
		//
		// // UpdateManager uManager = new
		// // UpdateManager(
		// // new_home_activity.this);
		// // uManager.apkUrl = GagApi.UPAPKPATH;
		// // uManager.checkUpdateInfo();
		// } else {
		// System.out.println("已经是最新的版本");
		// //
		// }
		// // findXq();
		//
		// } else {
		// // System.out.println(state.getMsg());
		// // ToastUtils.showLong(state.getMsg());
		// Toast.makeText(SettingActivity.this, "已经是最新的版本",
		// Toast.LENGTH_LONG).show();
		// }
		// }
		//
		// public void onFailure(HttpException error, String msg) {
		// // System.out.println(msg);
		// }
		// });
		// } catch (NameNotFoundException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	private void showDonwLoad() {
		InPutDialog inPutDialog = new InPutDialog(SettingActivity.this, "版本更新",
				"有新的版本更新", "下载", "取消");
		inPutDialog.setGetUserIntent(new getUserIntent() {

			@Override
			public void getUserIntentInfo() {
				// TODO Auto-generated method stub
				Intent intents = new Intent();
				intents.setClass(SettingActivity.this, DownLoadAPP.class);
				String root = SD_Finder.GetPaht();
				if (root != null && root.length() > 0) {
					intents.putExtra("path", root);
					startService(intents);

				} else {
					Toast.makeText(SettingActivity.this, "未找到SD卡，无法下载",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		inPutDialog.getCheckDialog(false);
	}

	private void exitCEHD() {
		LocationClient mLocationClient = app.mLocationClient;
		mLocationClient.stop();
		// PushManager.stopWork(getApplicationContext());
		JPushInterface.stopPush(getApplicationContext());
		SPUtils.remove(SettingActivity.this, "userPhone");
		SPUtils.remove(SettingActivity.this, "userPassword");
		SPUtils.clear(SettingActivity.this);
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.cancel(10);
		app.onTerminate();
		int currentVersion = android.os.Build.VERSION.SDK_INT;
		if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
			Intent startMain = new Intent(Intent.ACTION_MAIN);
			startMain.addCategory(Intent.CATEGORY_HOME);
			startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(startMain);
			System.exit(0);
		} else {// android2.1
			ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
			am.restartPackage(getPackageName());
		}

	}

}
